// 联合类型
function padLeft(value: string, padding: any){
    if(typeof padding === "number"){
        return Array(padding+1).join("0")+value;
    }
    else if(typeof padding === "string")
        return padding+value;
    else
        throw new Error("Expected string or number");
}

document.write("<p>"+padLeft("Hello", 4)+"</p>");
document.write("<p>"+padLeft("Hello", "0000")+"</p>");

function padLeft2(value: string, padding: number|string){
    if(typeof padding === "number"){
        return Array(padding+1).join("0")+value;
    }
    else if(typeof padding === "string")
        return padding+value;
    else
        throw new Error("Expected string or number");
}
// 传入对象报错
//document.write("<p>"+padLeft2("Hello", {"Name":"zha"})+"</p>");

interface Bird{
    fly();
    layEggs();
}
interface Fish{
    swim();
    layEggs();
}
function getSmallPet(): Fish|Bird{
    let b:Bird;
    return b;
}

let pet = getSmallPet();
pet.layEggs();
//pet.swim(); // 返回的bird中无swim方法

// 使用类型断言
function isFish(pet: Fish|Bird):pet is Fish{
    return ((<Fish>pet).swim) !== undefined;
}

// instanceof 


// 交叉类型
function myExtend<T,U>(first: T, second: U):T&U{
    let result = <T&U>{};

    for(let id in first){
        (<any>result)[id] = (<any>first)[id];
    }

    for(let id in second){
        if(!result.hasOwnProperty(id)){
            (<any>result)[id] = (<any>second)[id];
        }
    }
    return result;
}

// 类型别名
type Name = string;
type NameReslvr = ()=>string;
type Container<T> = {value:T};

// 字符串字面量类型
type Easing = "easy-in"|"ease-out"|"ease-in-out";
function animate(easiing: Easing){
    //...
}

















